package exception;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public ModelAndView handleAllExceptions(Exception ex, HttpServletRequest request) {
        ModelAndView modelView = new ModelAndView("error");
        modelView.addObject("errorMsg", ex.getMessage());
        logger.info("异常信息: {}", ex.getMessage());
        logger.info("errorMsg 设置为: {}", ex.getMessage());
        ex.printStackTrace();
        return modelView;
    }
}